package com.lanmaoly.stream;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class Test2 {

    private static ThreadLocalRandom random = ThreadLocalRandom.current();

    public static void main(String[] args) {
        Test2 test2 = new Test2();
//        test2.streamTest1();
//        test2.test2();
    }

    private void test3() {
        Random random1 = new Random();
        int i = random1.nextInt(10);
    }

    private void test2() {
        List<Person> list = new ArrayList<>(16);
        list.add(this.getPerson("lisi"));
        list.add(this.getPerson("zhangsan"));
        list.add(this.getPerson("wangwu"));
        list.add(this.getPerson("zhaoliu"));

        list.sort(Comparator.comparingInt(Person::getAge).reversed());
        for (Person person : list) {
            System.out.println(person.toString());
        }
    }

    private void streamTest1() {
        List<Person> list = new ArrayList<>(16);
        list.add(this.getPerson("lisi"));
        list.add(this.getPerson("zhangsan"));
        list.add(this.getPerson("wangwu"));
        list.add(this.getPerson("wangwu"));
        list.add(this.getPerson("zhaoliu"));
        list.add(this.getPerson("zhaoliu"));
        System.out.println(list.toString());

        List<String> stringList = list.stream().map(Person::getName).collect(Collectors.toList());
        System.out.println(stringList.toString());

        Set<String> stringSet = list.stream().map(Person::getName).collect(Collectors.toSet());
        System.out.println(stringSet.toString());
    }

    private Person getPerson(String name) {
        Person person = new Person();
        person.setAge(random.nextInt(30));
        person.setName(name);
        person.setSex(random.nextBoolean());
        return person;
    }
}
